package cn.zr.aitool.fragments;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.zr.aitool.R;
import cn.zr.aitool.bean.ContentBean;
import cn.zr.aitool.factory.ModelFactory;
import cn.zr.aitool.fragments.ItemFragment.OnListFragmentInteractionListener;
import cn.zr.aitool.model.IContentModel;


public class ContentItemAdapter extends RecyclerView.Adapter<ContentItemAdapter.ViewHolder> {

    private List<ContentBean> mValues;
    private final OnListFragmentInteractionListener mListener;
    private int[] colors;
    private Context mContext;
    private int[] topImages;
    private int mIndex;

    public ContentItemAdapter(List<ContentBean> items, OnListFragmentInteractionListener listener, int index) {
        mIndex = index;
        mValues = items;
        mListener = listener;
        initColor();
    }

    public void setData(List<ContentBean> items){
        mValues.clear();
        mValues.addAll(items);
    }

    public void initColor() {
        colors = new int[2];
        colors[0] = R.color.item_top_blue;
        colors[1] = R.color.item_top_red;
        topImages = new int[2];
        topImages[0] = R.mipmap.label_blue;
        topImages[1] = R.mipmap.label_green;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        IContentModel contentModel = ModelFactory.getContenModel(mIndex, holder.mItem);
        holder.mIdView.setText(holder.mItem.title);
        holder.mContentView.setText(holder.mItem.content);

        setOrHideView(holder.mItem.start, holder.mStartTv);
        setOrHideView(holder.mItem.end, holder.mEndTv);
        setOrHideView(holder.mItem.content1, holder.mContentView1);
        setOrHideView(holder.mItem.content2, holder.mContentView3);

        holder.mContentView2.setText(contentModel.getDescribeByType());

        holder.mIvTopImage.setImageDrawable(mContext.getResources().getDrawable(topImages[holder.mItem.type]));
        holder.mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.onListFragmentInteraction(holder);
                }
            }
        });

        holder.mCardView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (null != mListener) {
                    mListener.onLongClick(holder);
                    return true;
                }
                return false;
            }
        });
    }

    public void setOrHideView(String text, TextView targetView) {
        if (TextUtils.isEmpty(text)) {
            targetView.setVisibility(View.GONE);
        } else {
            targetView.setVisibility(View.VISIBLE);
            targetView.setText(text);
        }
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mContentView;
        public final TextView mContentView1;
        public final TextView mContentView2;
        public final TextView mContentView3;
        public final CardView mCardView;
        public final TextView mStartTv;
        public final TextView mEndTv;
        public final LinearLayout mLlRoot;
        public final ImageView mIvTopImage;

        public ContentBean mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = view.findViewById(R.id.title);
            mContentView = view.findViewById(R.id.content);
            mContentView1 = view.findViewById(R.id.content1);
            mContentView2 = view.findViewById(R.id.content2);
            mContentView3 = view.findViewById(R.id.content3);
            mCardView = view.findViewById(R.id.cardView);
            mStartTv = view.findViewById(R.id.time_start);
            mEndTv = view.findViewById(R.id.time_end);
            mLlRoot = view.findViewById(R.id.item_root);
            mIvTopImage = view.findViewById(R.id.item_top_image);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }

}
